package vg.shared.graph.utils;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import vg.interfaces.ll_services.data_base_service.data.graph.Attribute;
import vg.interfaces.ll_services.data_base_service.data.graph.Edge;
import vg.interfaces.ll_services.data_base_service.data.graph.Graph;
import vg.interfaces.ll_services.data_base_service.data.graph.Vertex;
import vg.interfaces.ll_services.data_base_service.data.header.EdgeHeader;
import vg.interfaces.ll_services.data_base_service.data.header.VertexHeader;
import vg.services.main_manager.VGMainServiceHelper;

import java.util.Collection;

/**
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class GraphUtils {
    private static final String DEFAULT_ATTRIBUTE = "name";

    public static BiMap<Vertex, Integer> orderVertices(Graph graph) {
        BiMap<Vertex, Integer> vertex2IndexBiMap = HashBiMap.create();

        if (graph == null)
            return vertex2IndexBiMap;

        Collection<Vertex> vertices = graph.getVertices();
        if (vertices != null && vertices.size() > 0) {
            int index = 0;
            for(Vertex v: vertices) {
                vertex2IndexBiMap.put(v, index);
                index++;
            }
        }

        return  vertex2IndexBiMap;
    }

    public static BiMap<Edge, Integer> orderEdges(Graph graph) {
        BiMap<Edge, Integer> edge2IndexBiMap = HashBiMap.create();

        if (graph == null)
            return edge2IndexBiMap;

        Collection<Edge> edges = graph.getEdges();
        if (edges != null && edges.size() > 0) {
            int index = 0;
            for(Edge e: edges) {
                edge2IndexBiMap.put(e, index);
                index++;
            }
        }

        return  edge2IndexBiMap;
    }

    public static String generateGraphTitle(Graph graph) {
        String defaultTitle = "unknown";

        if(graph == null)
            return defaultTitle;

        int graphId = graph.getStorableId();

        if (graphId < 0) {
            for (Vertex vertex : graph.getVertices()) {
                if (vertex.getStorableId() > 0) {
                    VertexHeader vertexHeader = VGMainServiceHelper.graphDataBaseManager.getVertexHeader(vertex.getStorableId());
                    graphId = vertexHeader.getGraphId();
                    break;
                }
            }
        }

        if (graphId < 0) {
            for (Edge edge : graph.getEdges()) {
                if (edge.getStorableId() > 0) {
                    EdgeHeader edgeHeader = VGMainServiceHelper.graphDataBaseManager.getEdgeHeader(edge.getStorableId());
                    graphId = edgeHeader.getGraphId();
                    break;
                }
            }
        }

        if (graphId >= 0) {
            VertexHeader vertexHeader = VGMainServiceHelper.graphDataBaseManager.getVertexHeader(graphId);
            if (vertexHeader != null) {
                Vertex vertex = VGMainServiceHelper.graphDataBaseManager.getVertex(vertexHeader.getId());
                Attribute attribute = vertex.getAttribute(DEFAULT_ATTRIBUTE);
                if (attribute != null)
                    return attribute.getStringValue();
            }
        }

        return defaultTitle;
    }
}
